# Contributor: Dhruvin Gandhi <contact@dhruvin.dev>
# Maintainer: Dhruvin Gandhi <contact@dhruvin.dev>
pkgname=hledger-stockquotes
pkgver=0.1.2.2
pkgrel=2
pkgdesc="hledger addon that pulls historical stock prices for commodities"
url="https://github.com/prikhi/hledger-stockquotes"
arch="x86_64" # limited by GHC
license="BSD-3-Clause"
makedepends="ghc cabal libffi-dev ncurses-dev zlib-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/prikhi/hledger-stockquotes/archive/refs/tags/$pkgver.tar.gz
	cabal.config
	"
options="!check" # fail to build for some reason

export CABAL_DIR="$srcdir"/cabal

cabal_update() {
	msg "Freezing $pkgname dependencies"

	# Resolve deps and generate fresh cabal.config with version constraints.
	(
		cd "$builddir"
		cabal update
		cabal v1-freeze --strong-flags --shadow-installed-packages

		# Add version tag at the first line.
		sed -i "1i--$pkgver" "cabal.config"

		mv "cabal.config" "$startdir/"
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	default_prepare

	if [ "$(head -n 1 "$srcdir/cabal.config")" != "--$pkgver" ]; then
		die "Requirements file is outdated, run 'abuild cabal_update'"
	fi

	ln -sf "$srcdir/cabal.config" "$builddir/cabal.project.freeze"
}

build() {
	cabal update
	cabal build --prefix=/usr --enable-relocatable
}

check() {
	cabal test
}

package() {
	local ghcver=$(ghc --version | cut -d " " -f 8)
	install -Dm755 \
		"dist-newstyle/build/$arch-linux/ghc-$ghcver/$pkgname-$pkgver/x/$pkgname/build/$pkgname/$pkgname" \
		"$pkgdir"/usr/bin/$pkgname
}

sha512sums="
0b637efc9d99f1b283b53cd45331603a8badd30d91692a854786e91c536ddf2d336973f7c2b8f1d8e439dc730893d91fbd1c2a040bed448a59f1006bfee79653  hledger-stockquotes-0.1.2.2.tar.gz
1f22cf36724aebc303d9f11c068d4778332364c5d55ab835b4ed757619ce9a4648601d0a85261172327a30fb923c3cd3492b52778998f70694cc889847539ddf  cabal.config
"
